﻿Imports Windows.UI
Imports Windows.UI.Popups

Public Class PopupMessage

    Public Title As String = ""
    Public Message As String = ""
    Public LeftButtonText As String = ""
    Public CentralButtonText As String = ""
    Public RightButtonText As String = ""

    Public LeftButtonColor As Color = Colors.Black
    Public CentralButtonColor As Color = Colors.Black
    Public RightButtonColor As Color = Colors.Black

    Public MessageStyle As MessagePopupStyle = Nothing

    Public isOpen As Boolean = False

    Private openDialogCommand As IUICommand
    Private WithEvents dialog As MessagePopup
    Private popup As Popup

    Public Enum enPopupButtonID
        LEFT_BUTTON = 0
        CENTRAL_BUTTON = 1
        RIGHT_BUTTON = 2
    End Enum
    Public Event ButtonPressed(buttonId As enPopupButtonID)


    Private Sub DialogLeftButton_Pressed()
        Me.isOpen = False
        popup.IsOpen = False
        RemoveHandler dialog.LeftButtonPressed, AddressOf DialogLeftButton_Pressed
        RemoveHandler dialog.CentralButtonPressed, AddressOf DialogCentralButton_Pressed
        RemoveHandler dialog.RightButtonPressed, AddressOf DialogRightButton_Pressed
        RaiseEvent ButtonPressed(enPopupButtonID.LEFT_BUTTON)
    End Sub

    Private Sub DialogCentralButton_Pressed()
        Me.isOpen = False
        popup.IsOpen = False
        RemoveHandler dialog.LeftButtonPressed, AddressOf DialogLeftButton_Pressed
        RemoveHandler dialog.CentralButtonPressed, AddressOf DialogCentralButton_Pressed
        RemoveHandler dialog.RightButtonPressed, AddressOf DialogRightButton_Pressed
        RaiseEvent ButtonPressed(enPopupButtonID.CENTRAL_BUTTON)
    End Sub

    Private Sub DialogRightButton_Pressed()
        Me.isOpen = False
        popup.IsOpen = False
        RemoveHandler dialog.LeftButtonPressed, AddressOf DialogLeftButton_Pressed
        RemoveHandler dialog.CentralButtonPressed, AddressOf DialogCentralButton_Pressed
        RemoveHandler dialog.RightButtonPressed, AddressOf DialogRightButton_Pressed
        RaiseEvent ButtonPressed(enPopupButtonID.RIGHT_BUTTON)
    End Sub

    Public Sub New()

    End Sub

    Public Sub New(message As String, title As String, leftButtonText As String, centralButtonText As String, rightButtonText As String)

        Me.Message = message
        Me.Title = title
        Me.LeftButtonText = leftButtonText
        Me.CentralButtonText = centralButtonText
        Me.RightButtonText = rightButtonText

    End Sub

    Public Sub Show()

        dialog = New MessagePopup(Message, Title, LeftButtonText, CentralButtonText, RightButtonText)
        AddHandler dialog.LeftButtonPressed, AddressOf DialogLeftButton_Pressed
        AddHandler dialog.CentralButtonPressed, AddressOf DialogCentralButton_Pressed
        AddHandler dialog.RightButtonPressed, AddressOf DialogRightButton_Pressed

        dialog.SetLeftButtonColor(Me.LeftButtonColor)
        dialog.SetCentralButtonColor(Me.CentralButtonColor)
        dialog.SetRightButtonColor(Me.RightButtonColor)

        If MessageStyle IsNot Nothing Then
            dialog.SetStyle(MessageStyle)
        End If

        popup = New Popup
        popup.Child = dialog

        popup.IsOpen = True
        Me.isOpen = True

        dialog.Focus(FocusState.Programmatic)
    End Sub

    Public Sub Close()
        Me.isOpen = False
        popup.IsOpen = False
    End Sub

End Class
